package com.example.javaee_blog.mapper;

import org.junit.jupiter.api.Test;
import org.springframework.util.DigestUtils;

import java.util.UUID;

public class SecurityUtiltest {
    @Test
    public void encrypt(){
        String password = "123456";
        String mdSStr = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println(mdSStr);
        //随机盐值
        String salt = UUID.randomUUID().toString().replace("-","");
        System.out.println(salt);
        //密文：盐值 + 明文 加密
        String securityPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        System.out.println(securityPassword);
        // salt + 密文 存储在数据库
        String finalPassword = salt + securityPassword;
        System.out.println(finalPassword);
    }
    @Test
    public void verify(){
        String password  = "123456";
        String sqlPassword = "aca580049884427d8b17e8cd281e21fabad99740de6b016eda52909bea35b90c";
        // sqlPassword: salt + 密文 存储在数据库
        if(sqlPassword == null || sqlPassword.length()!=64){
            System.out.println("校验失败");
        }
        String salt = sqlPassword.substring(0, 32);
        String securityPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        if((salt+securityPassword).equals(sqlPassword)){
            System.out.println("校验成功");
        }else{
            System.out.println("失败");
        }
    }
}
